package com.ltv.xiangqiplugin.controllers;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.Vector;

import com.ltv.xiangqiplugin.models.Destination;
import com.ltv.xiangqiplugin.utils.MoveUtils;
import com.ltv.xiangqiplugin.views.BoardView;
import com.ltv.xiangqiplugin.views.ChessApp;

public class BoardViewControl extends MouseAdapter {
	private BoardView.PiecePanel originPanel;
	private Vector<Destination> possibleMoves;
	private ChessApp mainApp;
	
	public BoardViewControl(ChessApp mainApp) {
		this.mainApp = mainApp;
		originPanel = null;
	}
	
	public void mouseClicked(MouseEvent event) {
		BoardView.PiecePanel thisPiece = (BoardView.PiecePanel)event.getSource();
		if (originPanel == null) {
			// Nhan vao o trong
			if (!thisPiece.occupied()) {
				return;
			}
			
			// Luot co do, nhung lai chon co xanh
			if (mainApp.getBoardView().getGame().isRedTurn() && Character.isLowerCase(thisPiece.getPiece())) {
				return;
			}
			
			// Luot co xanh, nhung lai chon co do
			if (mainApp.getBoardView().getGame().isBlueTurn() && Character.isUpperCase(thisPiece.getPiece())) {
				return;
			}
			
			// OK
			mainApp.getBoardView().clearHighlights();
			mainApp.getBoardView().clearLastMoves();
			originPanel = thisPiece;
			possibleMoves = mainApp.getBoardView().getGame().possibleMoves(originPanel.getCoord(), true);
			originPanel.highlight(BoardView.H_SEL);
			for (Enumeration<Destination> e = possibleMoves.elements();
					e.hasMoreElements();) {
				Destination dest = (Destination)e.nextElement();
				short highlight = 0;
				if (dest.isCapture()) {
					highlight = BoardView.H_CAPTURE;
				} else {
					highlight = BoardView.H_DEST;
				}
				mainApp.getBoardView().getPiecePanel(dest.algCoord()).highlight(highlight);
			}
			return;
		} else {
			// Chon quan co da duoc chon
			if (originPanel == thisPiece) {
				mainApp.getBoardView().clearHighlights();
				mainApp.getBoardView().drawMove(mainApp.getMoveStack().getMoves().get(mainApp.getMoveStack().getIndex()));
				originPanel = null;
				return;
			}
			
			// Chon quan co khac (doi y)
			if (originPanel.isFriend(thisPiece)) {
				originPanel = thisPiece;
				mainApp.getBoardView().clearHighlights();
				
				originPanel = thisPiece;
				possibleMoves = mainApp.getBoardView().getGame().possibleMoves(originPanel.getCoord(), true);
				originPanel.highlight(BoardView.H_SEL);
				for (Enumeration<Destination> e = possibleMoves.elements();
						e.hasMoreElements();) {
					Destination dest = (Destination)e.nextElement();
					short highlight = 0;
					if (dest.isCapture()) {
						highlight = BoardView.H_CAPTURE;
					} else {
						highlight = BoardView.H_DEST;
					}
					mainApp.getBoardView().getPiecePanel(dest.algCoord()).highlight(highlight);
				}
				return;
			}
			
			// Make move
			String move = originPanel.getCoord()+"-"+thisPiece.getCoord();
			String vnMove = MoveUtils.getVnMove(move, mainApp.getBoardView().getGame());
			
			if (mainApp.getBoardView().getGame().move(move, true)) {
				if (!mainApp.getGameActionView().isEnableRollback()) {
					mainApp.getGameActionView().enableRollbackButton();
					ChessApp.setBufferedIndex(mainApp.getMoveStack().getIndex());
				}
				
				originPanel = null;
				mainApp.getPositionStack().addPostion(mainApp.getBoardView().getGame().getPosition());
				mainApp.getMoveStack().addMove(move);
				mainApp.getMoveStack().addVnMove(vnMove);
				mainApp.getMoveStack().notifyObservers(true);
				
				mainApp.getBoardView().clearHighlights();
				mainApp.getBoardView().drawMove(mainApp.getMoveStack().getMoves().get(mainApp.getMoveStack().getIndex()));
				return;
			}
		}
	}
	
	public void setOriginPanel(BoardView.PiecePanel originPanel) {
		this.originPanel = originPanel;
	}
	
}
